# coding=utf-8
import json

from server.base.tools.cachedataclient import CacheDataClient

"""
缓存数据对象
"""


class CacheData:
    def __init__(self):
        self.id = None
        self.ip = None
        self.state = "Online"
        self.cmds = []

    def reg(self, id, ip):
        '''
        注册客户端到主机上
        :param clientId:
        :return:
        '''

        cacheDataClient = CacheDataClient()

        try:
            arrJson = cacheDataClient.read()
            arr = json.loads(arrJson)
        except:
            arr = []

        self.id = id
        self.ip = ip
        if arr.__len__() != 0:
            for data in arr:
                cacheData = CacheData()
                cacheData.__dict__.update(data)
                if cacheData.id == id:
                    arr.remove(data)
                    break

        arr.append(self.__dict__)
        cacheDataClient.write(json.dumps(arr, ensure_ascii=False, encoding='UTF-8'))

    def listClients(self):
        '''
        读取所有客户端
        :return:
        '''

        cacheDataClient = CacheDataClient()
        arrJson = cacheDataClient.read()
        arr = json.loads(arrJson)
        if not arr:
            return []

        return arr

    def pullCmd(self, cmd=None, id=None):
        '''
        执行指令
        :param cmd:
        :param clientId:
        :return:
        '''
        cacheDataClient = CacheDataClient()
        arrJson = cacheDataClient.read()
        arr = json.loads(arrJson)
        for data in arr:
            cacheData = CacheData()
            cacheData.__dict__.update(data)
            if cacheData.id == id:
                cacheData.cmds.append(cmd)
                cacheDataClient.write(json.dumps(arr))
                break

    def removeCmd(self, cmd=None, id=None):
        '''
       删除已经执行的指令
       :param cmd:
       :param clientId:
       :return:
       '''
        try:
            cacheDataClient = CacheDataClient()
            arrJson = cacheDataClient.read()
            arr = json.loads(arrJson)
            for data in arr:
                cacheData = CacheData()
                cacheData.__dict__.update(data)
                if cacheData.id == id:
                    cacheData.cmds.remove(cmd)
                    cacheDataClient.write(json.dumps(arr))
                    return True
        except:
            return False

    def getCmd(self, id=None):
        '''
        获得一个指令
        :param id:
        :return:
        '''
        cacheDataClient = CacheDataClient()
        arrJson = cacheDataClient.read()
        arr = json.loads(arrJson)
        for data in arr:
            cacheData = CacheData()
            cacheData.__dict__.update(data)
            if cacheData.id == id:
                if cacheData.cmds.__len__() != 0:
                    return cacheData.cmds[0]

    def checkState(self, id=None, state="Offline"):
        '''
        上线下线检测
        :param id: 客户端id
        :param state: 状态 Online|Offline
        :return:
        '''
        cacheDataClient = CacheDataClient()
        arrJson = cacheDataClient.read()
        arr = json.loads(arrJson)
        for data in arr:
            cacheData = CacheData()
            cacheData.__dict__.update(data)
            if cacheData.id == id:
                cacheData.state = str(state)
                arr.remove(data)
                arr.append(cacheData.__dict__)
                cacheDataClient.write(json.dumps(arr))
                break

    def toObj(self, value):
        '''
        转化成CacheData对象
        :param value:json字符串
        :return:CacheData对象
        '''
        self.__dict__ = json.loads(value)
        return self


if __name__ == '__main__':
    beanret = CacheData()
    beanret.clientIds.append(1)

    print(beanret.toJson())
    jsonStr = json.dumps(beanret.__dict__)
    print(jsonStr)
    beanretJson = json.loads(jsonStr)
    beanret2 = CacheData()
    beanret2.toObj(jsonStr)
